import pymysql


class getData():
    def env(self):
        pass

    def alterLastDate(self):
        db = pymysql.Connect("81.68.254.79", "root", "6DXb7JSQ6yYHd6Hi4t8jBGJZlcR3QomnzF3cGXKTawk=", "9acmdb",
                             charset='utf8', cursorclass=pymysql.cursors.DictCursor, autocommit=True)
        cursor = db.cursor()
        cursor.execute("select  from_unixtime(activetime,'%Y-%m-%d %H:%i:%s') time,alertname,hostname,description,value from collectAlarm  where  state='1' order by activetime desc limit 50;")
        rows = cursor.fetchall()
        db.close()
        return (rows)

    def alterInfoDate(self):
        db = pymysql.Connect("81.68.254.79", "root", "6DXb7JSQ6yYHd6Hi4t8jBGJZlcR3QomnzF3cGXKTawk=", "9acmdb",
                             charset='utf8', cursorclass=pymysql.cursors.DictCursor, autocommit=True)
        cursor = db.cursor()
        cursor.execute("select from_unixtime(activetime,'%Y-%m-%d %H:%i:%s') time,alertname,hostname,description,value from collectAlarm where state='1' order by severity,alertname;")
        rows = cursor.fetchall()
        db.close()
        return (rows)

    def getFilterTable(self):
        db = pymysql.Connect("81.68.254.79", "root", "6DXb7JSQ6yYHd6Hi4t8jBGJZlcR3QomnzF3cGXKTawk=", "9acmdb",
                             charset='utf8', cursorclass=pymysql.cursors.DictCursor, autocommit=True)
        cursor = db.cursor()
        cursor.execute("select * from AlarmFilter;")
        rows = cursor.fetchall()
        db.close()
        return (rows)


class changeDate():
    def deleteFilterTable(self, id):
        db = pymysql.Connect("81.68.254.79", "root", "6DXb7JSQ6yYHd6Hi4t8jBGJZlcR3QomnzF3cGXKTawk=", "9acmdb",
                        charset='utf8', cursorclass=pymysql.cursors.DictCursor, autocommit=True)
        cursor = db.cursor()
        self.id=id
        cursor.execute("delete from AlarmFilter where id='{}';".format(self.id) )
        db.commit()
        db.close()

    def addFilterTable (self, filterName, filterHost, filterRedmin):
        db = pymysql.Connect("81.68.254.79", "root", "6DXb7JSQ6yYHd6Hi4t8jBGJZlcR3QomnzF3cGXKTawk=", "9acmdb",
                             charset='utf8', cursorclass=pymysql.cursors.DictCursor, autocommit=True)
        cursor = db.cursor()
        cursor.execute("insert into AlarmFilter (filterName,filterHost,filterRedmin) values ('{}','{}','{}');".format(filterName, filterHost, filterRedmin))
        db.commit()
        db.close()

    def editFilterTable(self, id, filterName, filterHost, filterRedmin):
        db = pymysql.Connect("81.68.254.79", "root", "6DXb7JSQ6yYHd6Hi4t8jBGJZlcR3QomnzF3cGXKTawk=", "9acmdb",
                             charset='utf8', cursorclass=pymysql.cursors.DictCursor, autocommit=True)
        cursor = db.cursor()

        cursor.execute("update  AlarmFilter set filterName='{}',filterHost='{}',filterRedmin='{}' where id='{}';".format(filterName, filterHost, filterRedmin, id))
        db.commit()
        db.close()

